// pages/video-music/index.js
import {getTopMV} from "../../server/api_video"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        topMvs: [],
        hasMore: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
      const res =  await getTopMV(0)
       this.setData({ topMvs: res.data })
        
    },

        // 网络请求的方法
   async getTopMVData(offset) {
    if(!this.data.hasMore) return
     // 展示加载动画
     wx.showNavigationBarLoading()

       // 真正请求数据
    const res =  await getTopMV(offset)
    let newData = this.data.topMvs
    if(offset === 0) {
        newData = res.data
    }else {
        newData = newData.concat(res.data)
    }
       // 设置数据
   this.setData({ topMvs: newData })
   this.setData({ hasMore: res.hasMore })
   wx.hideNavigationBarLoading()
   if (offset === 0) {
     wx.stopPullDownRefresh()
   }
},

     // 上拉刷新
    onPullDownRefresh:  function() {
       this.getTopMVData(0)
    },

    // 下拉刷新
    onReachBottom: async function() { 
        // const res = await getTopMV(this.data.topMvs.length)
        // const Count = [... this.data.topMvs, ...res.data]
        // this.setData({ topMvs: Count })
        // this.setData({ hasMore: res.hasMore })
        this.getTopMVData(this.data.topMvs.length)
    },

    // 点击事件
    handleVideoItemClick(event) {
        const id = event.currentTarget.dataset.item.id
        wx.navigateTo({
            url: `/pages/detail-video/index?id=${id}`,
          })
    }
})